<?php

/**
 * 获取高销量商品, 不判断是否有券, 每小时更新一次
 */
// 定义网站根目录.
define('ROOT_PATH', realpath(__DIR__ . '/../'));
// 定义网站应用目录.
define('APP_PATH', ROOT_PATH . '/App/');
// 定义网站缓存文件目录.
define('CACHE_PATH', ROOT_PATH . '/Caches/');

// 加载应用入口文件.
require_once APP_PATH . 'App.php';
App::autoload();

// 最低佣金比率
$minCommissionRate = "700";
// 最低月销量
$minVolume = "666";
// 最低红包金额
$minPacket = \Config\System::$site['minPacket'];
// 不是必须包含优惠券
$hasCoupon = false;

// 分类黑名单
$cats_in_black = ['2813', '50023717', '50025707'];

// 无限循环, 常注内存
while (true) {
    // 取出所有分类
    $cats = json_decode(file_get_contents(ROOT_PATH . '/Data/cats.dat'), true);

    foreach ($cats as $c) {
        $cid = $c['cid'];

        if (in_array($cid, $cats_in_black)) {
            continue;
        }

        echo "更新分类: {$c['name']}({$cid})\n";
        $page = 1;

        while (true) {

            if ($page > 150) {
                // 每个分类最多读取100页数据
                break;
            }

            $resp = \Logic\TaoApi::getProducts($cid, '', $minCommissionRate, $hasCoupon, $page, 100, 'total_sales_des', true, true);
            if (empty($resp)) {
                $page++;
                continue;
            }

            if (isset($resp->result_list) && empty($resp->result_list)) {
                // 没有找到记录
                break;
            }

            $rs = $resp->result_list->map_data;
            if (!$rs) {
                break;
            }

            foreach ($rs as $r) {
                $productId = $r->num_iid;
                $count = \Db::instance()->count("select count(1) from `blacklist` where `product_id` = '$productId'");
                if ($count) {
                    // 黑名单跳过
                    //\Logic\Product::delete($productId);
                    continue;
                }

                if ($r->volume < $minVolume) {
                    // 月销量小于最低要求, 跳过本分类
                    break 2;
                }

                $coupon_id = isset($r->coupon_id) ? $r->coupon_id : '';
                $coupon_remain_count = isset($r->coupon_remain_count) ? $r->coupon_remain_count : 0;
                $coupon_start_time = isset($r->coupon_start_time) ? strtotime($r->coupon_start_time . ' 00:00:00') : 0;
                $coupon_end_time = isset($r->coupon_end_time) ? strtotime($r->coupon_end_time . ' 23:59:59') : 0;
                $coupon_share_url = isset($r->coupon_share_url) ? \Logic\Coupon::cleanCouponUrl($r->coupon_share_url) : '';
                $amount = isset($r->coupon_info) ? \Logic\Coupon::getAmount($r->coupon_info) : 0;
                $user_type = isset($r->user_type) ? $r->user_type : 0;

                // 优惠券剩余不多或时间无效
                $time = time();
                if ($coupon_share_url || $amount) {
                    if ($coupon_remain_count < 10 || $coupon_start_time > $time || $coupon_end_time <= $time) {
                        $coupon_share_url = '';
                        $amount = 0;
                    }
                }

                $click_url = \Logic\Product::cleanClickUrl($r->url);
                if (!$click_url) {
                    continue;
                }

                $commission_rate = $r->commission_rate < $minCommissionRate ? $minCommissionRate : $r->commission_rate;
                $zk_final_price = $r->zk_final_price;

                // 计算红包, 到手价等信息
                $more = \Logic\Product::getMore($zk_final_price, $commission_rate, $amount);

                // 红包小于要求
                if ($more['red_packet'] < $minPacket) {
                    \Logic\Product::delete($productId);
                    continue;
                }

                // 商品数据更新
                $product = [
                    'product_id' => $productId,
                    'pict_url' => $r->pict_url,
                    'title' => $r->title,
                    'zk_final_price' => $r->zk_final_price,
                    'commission_rate' => $commission_rate,
                    'coupon_id' => $coupon_id,
                    'coupon_share_url' => $coupon_share_url,
                    'coupon_remain_count' => $coupon_remain_count,
                    'coupon_start_time' => $coupon_start_time,
                    'coupon_end_time' => $coupon_end_time,
                    'volume' => $r->volume,
                    'red_packet' => $more['red_packet'],
                    'amount' => $more['amount'],
                    'final_price' => $more['final_price'],
                    'click_url' => $click_url,
                    'uptime' => time(),
                    'user_type' => $user_type,
                ];

                if (!\Logic\Product::insert($product)) {
                    $ret = \Logic\Product::update($product, $productId);
                    if (!$ret) {
                        //var_dump($ret);
                        echo "cid: {$cid}, page: {$page}, update product {$productId} failed\n";
                    }
                }
            }

            $page++;
        }
    }
    echo "up product end\n";

    // 休眠60分钟
    sleep(3600);
}